/*
Scripts to create interactive scrollbars in SVG using ECMA script
Copyright (C) 2008  -- Zach Banks
Version 0.8.1 -- June 1st 2008
zach@rethinker-industries.com
http://revecter.googlecode.com


-------

This ECMA script library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library (lesser_gpl.txt); if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

----

original document site: http://revecter.googlecode.com
Please contact the author in case you want to use code or ideas commercially.
If you use this code, please include this copyright header, the included full
LGPL 2.1 text and read the terms provided in the LGPL 2.1 license
(http://www.gnu.org/copyleft/lesser.txt)

-------------------------------

Please report bugs and send improvements to zach@rethinker-industries.com
If you use this control, please link to the original (http://revecter.googlecode.com)
somewhere in the source-code-comment or the "about" of your project and give credits, thanks!

*/


loader = function(name){
    this.name = name;
    window.loadingObjName = name;
    this.getDef();
    this.makeLoader();
}

loader.prototype.makeLoader = function(){
    var loadSymbolG = document.createElementNS(svgNS, "g");
    loadSymbolG.setAttributeNS(null, "transform", "translate("+
      document.documentElement.width.animVal.value/2
      +","+document.documentElement.height.animVal.value/3+")");
    
    this.loadSymbol = document.createElementNS(svgNS, "use");
    this.loadSymbol.setAttributeNS(xlinkNS, "href", "#" + this.symbol);
    this.loadSymbol.setAttributeNS(null, "transform", "rotate(0) translate(-32, -32)");
    this.loadSymbolRotation = 0;
    
    //loadSymbolG.setAttributeNS(revecterNS, "id", revecterItems.length);
    loadSymbolG.setAttributeNS(revecterNS, "type", "loading");
    //revecterItems.push({type:"loading", node:loadSymbolG});
    loadSymbolG.appendChild(this.loadSymbol);
    this.revLoading.parentNode.replaceChild(loadSymbolG, this.revLoading);
}

loader.prototype.getDef = function(){
    var defsSection = document.getElementsByTagName("defs")[0];
    if (!defsSection) {
        defsSection = document.createElementNS(svgNS,"defs");
        document.documentElement.appendChild(defsSection);
    }
    //there should only be one
    this.revLoading = document.getElementsByTagNameNS(revecterNS,"loading")[0];
    this.symbol = this.revLoading.getAttributeNS(null, "symbol") || "loadingSpinner";
    if (!document.getElementById(this.symbol)){
       var symbol = document.createElementNS(svgNS, "symbol");
       symbol.setAttributeNS(null, "id", this.symbol);
       symbol.setAttributeNS(null, "overflow", "visible");
       defsSection.appendChild(symbol);
       //line is a rect -- saves a variable
       line = document.createElementNS(svgNS, "path");
       line.setAttributeNS(null, "d", "M 0,30 C 0,15 16,0 32,0 C 48,0 64,16 64,30 L 54,25 M 64.5,33.5 C 64.5,48.5 48.5,63.5 32.5,63.5 C 16.5,63.5 0.5,47.5 0.5,33.5 L 10.5,38.5");
       line.setAttributeNS(null, "fill", "none");
       line.setAttributeNS(null, "stroke", "#012345");
       line.setAttributeNS(null, "stroke-width", "1");
       line.setAttributeNS(null, "pointer-events", "none");
       symbol.appendChild(line);
    }
    
}

loader.prototype.start = function(){
    //Timer()?
    var _this = this;
    this.loadingTimer = window.setInterval(function(){window[_this.name].loadSymbolRotation += 10;
    window[_this.name].loadSymbol.setAttributeNS(null, "transform", "rotate(" + 
    window[_this.name].loadSymbolRotation + ") translate(-32, -32)");}, 80);
}
loader.prototype.stop = function(){
    clearInterval(this.loadingTimer);
    window[this.name].loadSymbol.style.display = "none";
}



